package com.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.fragment.RecyclerViewFragment;
import com.util.R;
import com.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/4/6.
 */
public class RecyclerViewActivity extends FragmentActivity {
    @Bind(R.id.tl_recyclerView)
    TabLayout mTabLayout;
    @Bind(R.id.vp_recyclerView)
    MyViewPager mViewPager;
    private String[] title;
    private List<Fragment> fragmentList;
    private MyPagerAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);
        ButterKnife.bind(this);
        initData();
    }



    private void initData() {
        title = getResources().getStringArray(R.array.tittles);
        fragmentList = new ArrayList<>();
        initViewPager();
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式
        for (int i = 0; i < title.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title[i]));//添加tab选项卡
        }
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来
        mTabLayout.setTabsFromPagerAdapter(mAdapter);
    }

    private void initViewPager() {
        RecyclerViewFragment fragment1 = new RecyclerViewFragment();
        Bundle arg1 = new Bundle();
        arg1.putInt("mode", 0);
        fragment1.setArguments(arg1);
        RecyclerViewFragment fragment2 = new RecyclerViewFragment();
        Bundle arg2 = new Bundle();
        arg2.putInt("mode", 1);
        fragment2.setArguments(arg2);
        RecyclerViewFragment fragment3 = new RecyclerViewFragment();
        Bundle arg3 = new Bundle();
        arg3.putInt("mode", 2);
        fragment3.setArguments(arg3);
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager(), title, fragmentList);
        mViewPager.setAdapter(mAdapter);
    }




    class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] TITLES;
        private List<Fragment> fragmentList;

        public MyPagerAdapter(FragmentManager fm, String[] TITLES, List<Fragment> fragmentList) {
            super(fm);
            this.TITLES = TITLES;
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }


    }
}
